<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="客户身份证号" prop="identity">
        <el-input
          v-model="queryParams.identity"
          placeholder="请输入客户身份证号"
          clearable
          style="width: 250px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="carList" v-show="showRentTable" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="车辆id" align="center" prop="id"/>
      <el-table-column label="车牌号" align="center" prop="carNumber"/>
      <el-table-column label="车架号" align="center" prop="frameNumber"/>
      <el-table-column label="品牌" align="center" prop="brand"/>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="颜色" align="center" prop="color"/>
      <el-table-column label="购买价格" align="center" prop="price"/>
      <el-table-column label="出租价格" align="center" prop="rentprice"/>
      <el-table-column label="出租押金" align="center" prop="deposit"/>
      <el-table-column label="描述" align="center" prop="description"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="handleRent(scope.row)"
            v-hasPermi="['business:car:edit']"
          >出租
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="showCarImgs(scope.row)"
            v-hasPermi="['business:car:remove']"
          >查看大图
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆对话框 -->
    <el-dialog :title="title" :visible.sync="showRentFrom" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="租车时间">
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择起租日期" v-model="form.beginDate" format="yyyy-MM-dd HH:mm:ss"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            style="width: 100%;"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2"> --</el-col>
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择归还日期" v-model="form.returnDate" style="width: 100%;"
                            format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </el-col>

        </el-form-item>

        <el-form-item label="出租单号" prop="rentSn">
          <el-input v-model="form.rentSn" readonly/>
        </el-form-item>

        <el-form-item label="身份证号" prop="identity">
          <el-input v-model="form.identity" readonly/>
        </el-form-item>

        <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="form.carNumber" readonly/>
        </el-form-item>

        <el-form-item label="车架号" prop="frameNumber">
          <el-input v-model="form.frameNumber" readonly/>
        </el-form-item>

        <el-form-item label="出租价格" prop="rentprice">
          <el-input v-model="form.rentprice" placeholder="请输入出租价格"/>
        </el-form-item>
        <el-form-item label="出租押金" prop="deposit">
          <el-input v-model="form.deposit" placeholder="请输入出租押金"/>
        </el-form-item>

        <el-form-item label="操作员" prop="opername">
          <el-input v-model="form.opername" readonly/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">出租</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="车辆图片" :visible.sync="isShowCarImgs" width="1200px" append-to-body>
      <el-carousel :interval="4000" type="card" height="400px">
        <el-carousel-item v-for="img in carImgs">
          <img :src="img" alt="车辆图片" style="width: 100%;height: 400px;">
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
  </div>


</template>

<script>

import {checkCustomerId, showCarImgs, toRent, doRent} from '@/api/business/rentCar'
import {listCar} from '@/api/business/car'
import {Message} from "element-ui";

export default {
  name: "RentCar",
  dicts: ['car_type'],
  data() {
    return {
      showRentFrom: false,
      carImgs: [],
      showRentTable: false,
      // 部门的集合
      depts: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆表格数据
      carList: [],
      // 弹出层标题
      title: "汽车出租",
      // 是否显示弹出层
      isShowCarImgs: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        identity: null,
        status: 0
      },
      // 表单参数
      form: {},

    };
  },
  created() {

  },
  methods: {
    /** 查询车辆列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
        // 打开列表
        this.showRentTable = true;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // 1.将客户的身份证号传入后端  后端做一个校验
      let identity = this.queryParams.identity;
      checkCustomerId({"identity": identity}).then(res => {
        // 身份验证成功 加载未出租的汽车列表
        this.queryParams.pageNum = 1;
        this.getList();
      });

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        identity: null,
        status: 0
      };
      this.total = 0;
      this.resetForm("queryForm");
      // 隐藏下面的表格
      this.showRentTable = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },


    /** 提交按钮 */
    submitForm() {
      // 将出租信息 发到后端 后端真的做出租业务
      doRent(this.form).then(res => {
        Message({
          message: res.msg,
          type: 'success'
        })
        // 关掉弹层
        this.showRentFrom = false;
        // 关闭表格
        this.resetQuery();
      });

    },
    handleRent(row) {
      // 1. 发送 一个请求到后端  生成一个唯一的出租单号 包括车辆的基本信息 填充到表单中
      // 2. 打开弹层
      toRent({"carId": row.id}).then(res => {
        // 填充到表单中
        this.form = {
          carNumber: res.data.carNumber,
          rentSn: res.data.rentSn,
          frameNumber: res.data.frameNumber,
          rentprice: res.data.rentprice,
          deposit: res.data.deposit,
          opername: res.data.opername,
          identity: this.queryParams.identity
        }
        // 打开弹层
        this.showRentFrom = true;
      });
    },
    showCarImgs(row) {
      // row 行数据
      let carNumber = row.carNumber;
      // 发一个请求去后端 将该车辆的图片 查出来 返回给前端 前端进行渲染即可
      showCarImgs({"carNumber": carNumber}).then(res => {
        this.carImgs = res.data;
        // 打开弹层
        this.isShowCarImgs = true;
      })

    }


  }
};
</script>
